﻿using SF.Common;
using SF.Ucenter.api.wxpay;
using SF.Ucenter.shallWeChat;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.mycenterusers
{
    public partial class index : System.Web.UI.Page
    {
        public string userNum;
        protected void Page_Load(object sender, EventArgs e)
        {
            #region Oauth授权获取到用户信息
            WebClient obj = new WebClient();
            //获取配置的请求参数
            IDictionary dict = ConfigurationManager.GetSection("weixinPay") as IDictionary;
            string reluri = Utils.UrlEncode(dict["ip_url"].ToString() + "/mycenterusers/index.aspx");  //""
            string code_uri = dict["code_uri"].ToString();
            string openid_uri = dict["openid_uri"].ToString();
            string user_uri = dict["user_uri"].ToString();         
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["code"]))
                {
                    string appNum = Request.QueryString["state"];
                    Model.sf_appidmanage appModel = new BLL.sf_appidmanage().GetModel(appNum);
                    string appSecret = appModel != null ? appModel.appsecret : "";
                    string str = obj.GetHtml(string.Concat(openid_uri, "appid=", appNum, "&secret=", appSecret, "&code=", Request.QueryString["code"], "&grant_type=authorization_code"));
                    code2openid p = SF.Ucenter.api.wxpay.JsonHelper.JsonDeserialize<code2openid>(str);
                    string str2 = obj.GetHtml(string.Concat(user_uri, "access_token=" + p.access_token + "&openid=" + p.openid + "&lang=zh_CN"));
                    userinfo p1 = SF.Ucenter.api.wxpay.JsonHelper.JsonDeserialize<userinfo>(str2);
                    //根据openid获取当前微信端的 用户对象
                    BLL.sf_user_info userBll = new BLL.sf_user_info();
                    Model.sf_user_info userInfo = new Model.sf_user_info();
                    userInfo = userBll.GetModelByOpenId(p.openid);
                    //对 用户对象 昵称、头像 赋值
                    userInfo.nickName = p1.nickname;
                    userInfo.photoUri = p1.headimgurl;
                    userInfo.province = p1.province;
                    userInfo.city = p1.city;
                    Session[DTKeys.SESSION_ADMIN_INFO] = userInfo;
                    Session.Timeout = 45;
                    //写入登录日志
                    Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig();
                    bool isup = userBll.Update(userInfo);

                    loading();
                }
                else
                {
                    string appNum = Request.QueryString["appnum"];
                    //snsapi_base 请求Oauth所需code
                    string open = string.Concat(code_uri, "appid=", appNum, "&redirect_uri=", reluri, "&response_type=code&scope=snsapi_userinfo&state=" + appNum + "#wechat_redirect");
                    Response.Redirect(open, false);
                }
            }
            catch (Exception ex)
            {
            }
            //BLL.sf_user_info uBll = new BLL.sf_user_info();
            //Model.sf_user_info userInfo = uBll.GetModel(1);
            //userInfo = userInfo == null ? uBll.GetModel(2) : userInfo;
            //Session[DTKeys.SESSION_ADMIN_INFO] = userInfo;
            //loading();
            #endregion
        }

        /// <summary>
        /// 加载 页面数据
        /// 部分数据 交由js加载
        /// </summary>
        public void loading()
        {
            Model.sf_user_info userInfo = Session[DTKeys.SESSION_ADMIN_INFO] as Model.sf_user_info;
            if (userInfo != null)
            {
                if (userInfo.sellModel != 0)
                {
                    userNum = userInfo.userNum.ToString();
                    this.imgPhonto.ImageUrl = userInfo.photoUri;
                    this.lblNickName.Text = userInfo.nickName;
                    this.lblUserNum.Text = userInfo.userNum;
                    this.lblJoinDate.Text = Convert.ToDateTime(userInfo.addTime).ToString("yyyy-MM-dd");
                }
            }
        }
    }
}